package mytool

import (
	"math"
	"sync"
)

func Td(f func([]float64) float64, r []float64, b []float64, t int) []float64 {
	var tp []float64
	for s := 0; s < t; s++ {
		tp = append(tp, b...)
		for i, _ := range b {
			copy(tp, b)
			tp[i] += r[i]
			b[i] += f(tp) - f(b)
		}
	}
	return b
}
func Tdt(f func([]float64) float64, r []float64, b []float64, t int) []float64 {
	var tp []float64
	var wt sync.WaitGroup
	for s := 0; s < t; s++ {
		wt.Add(len(b))
		tp = append(tp, b...)
		for i, _ := range b {
			go func(i int) {
				defer wt.Done()
				copy(tp, b)
				tp[i] += r[i]
				b[i] += f(tp) - f(b)
			}(i)
		}
		wt.Wait()
	}
	return b
}

func Tdtg(f func([]float64) float64, r []float64, b []float64, t int) []float64 {
	var tp []float64
	var wt sync.WaitGroup
	ls := 0.0
	for s := 0; s < t; s++ {
		wt.Add(len(b))
		tp = append(tp, b...)
		for i, _ := range b {
			go func(i int) {
				defer wt.Done()
				copy(tp, b)
				tp[i] += r[i]
				ls = f(tp) - f(b)
				b[i] += ls / (math.Abs(ls) + 1)
			}(i)
		}
		wt.Wait()
	}
	return b
}
